package io.github.hadyang.leetcode.offer;

import org.junit.Test;

/** @author haoyang.shi */
public class ReverseSentence {

  @Test
  public void test() {
    System.out.println(ReverseSentence("student. a am I"));
    System.out.println(ReverseSentence(" "));
  }

  public String ReverseSentence(String str) {
    if (str == null || str.trim().equals("")) return str;

    String[] split = str.split(" ");

    StringBuilder builder = new StringBuilder();
    for (int i = split.length - 1; i >= 0; i--) {
      builder.append(split[i]);

      if (i != 0) builder.append(" ");
    }

    return builder.toString();
  }
}
